//
//  ViewController.m
//  26-GCD
//
//  Created by 曹曹 on 16/8/18.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *topImageView;
@property (weak, nonatomic) IBOutlet UIImageView *bottomImageView;
@property (nonatomic, strong) UIImage *topImage;
@property (nonatomic, strong) UIImage *bottomImage;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 1.什么是GCD?
    // Grand Central Dispatch - 大中央调度
    // 2.GCD的核心概念是什么 - 队列,任务拆分, 分配到队列中, 系统会自动为我们创建线程和调度线程 - 自动
    // 3.GCD中的队列严格遵循 - FIFO(先进先出)原则
    //  队列分类
    //  1>串行 - DISPATCH_QUEUE_SERIAL / NULL
    //  2>并发 - DISPATCH_QUEUE_CONCURRENT
    // 4.队列的创建
    //  术语:
    //  串行: 一个任务执行完毕, 在执行下一个
    //  并发: 多个任务同时执行
    //  1>串行
    //  参数1: 队列的名字
    //  参数2: 队列的类型
    dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
    //  2>并发
    dispatch_queue_t concurrentQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_CONCURRENT);
    // 系统提供的队列
    // 主队列 - 串行
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    NSLog(@"mainQueue:%@", mainQueue);
    // 全局队列 - 并发
    // 参数1: 调度队列优先级
    // 参数2: 给未来使用, 占位, 默认填0
    dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    // 5.同步 异步
    // 同步: 在当前线程执行任务, 不能开辟线程
    // 异步: 在新的线程执行任务, 能开辟线程
#pragma mark - 同步
    // 同步串行队列, 不会创建线程
    [self syncSerialQueue:serialQueue];
    // 同步并发队列, 不会创建线程
    [self syncConcurrentQueue:concurrentQueue];
#if 0
    // 同步主队列
    // 主线程同步主线程 - 主线程等待同步操作结束, 同步操作在主线程执行, 互相等待, 导致程序卡死
    [self syncMainQueue:mainQueue];
#endif
    // 同步全局队列, 不会创建线程
//    [self syncGlobalQueue:globalQueue];
#pragma mark - 异步
    // 异步串行队列 创建一个新的线程, 在该线程执行任务
//    [self asyncSerialQueue:serialQueue];
    // 异步并发队列 根据任务决定需要创建多个线程
//    [self asyncConcurrentQueue:concurrentQueue];
    // 异步主线程队列
//    [self asyncMainQueue:mainQueue];
    // 异步全局队列
    [self asyncGlobalQueue:globalQueue];
#pragma mark - dispatch group
    // 6.group 组
    //  1>组的创建
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t newConcurrentQueue1 = dispatch_queue_create("com.Fulu5.github", DISPATCH_QUEUE_CONCURRENT);
    //  2>组只有异步
    //    组中添加多个任务
    dispatch_group_async(group, newConcurrentQueue1, ^{
        NSLog(@"group1-----%@", [NSThread currentThread]);
    });
    dispatch_queue_t newConcurrentQueue2 = dispatch_queue_create("com.Fulu5.github", DISPATCH_QUEUE_CONCURRENT);
    dispatch_group_async(group, newConcurrentQueue2, ^{
        NSLog(@"group2-----%@", [NSThread currentThread]);
    });
    dispatch_queue_t newConcurrentQueue3 = dispatch_queue_create("com.Fulu5.github", DISPATCH_QUEUE_CONCURRENT);
    dispatch_group_async(group, newConcurrentQueue3, ^{
        NSLog(@"group3-----%@", [NSThread currentThread]);
    });
    //    所有任务都执行完毕, 会调用下边的方法
    dispatch_group_notify(group, mainQueue, ^{
        NSLog(@"所有操作完成!");
    });
    //http://pic1.win4000.com/wallpaper/8/511ce1c9ee276.jpg
    //http://www.lingyu.me/wp-content/uploads/2016/08/www.lingyu.me_20160808081654350.jpg
}
- (IBAction)GCDAction:(id)sender {
    // 自己创建的并发队列
    dispatch_queue_t concurrentQueue = dispatch_queue_create("com.Fulu5.github", DISPATCH_QUEUE_CONCURRENT);
    // group
//    dispatch_group_t group = dispatch_group_create();
//    dispatch_group_async(group, concurrentQueue, ^{
//        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://pic1.win4000.com/wallpaper/8/511ce1c9ee276.jpg"]];
//        UIImage *image = [UIImage imageWithData:data];
//        self.topImage = image;
//    });
//    dispatch_group_async(group, concurrentQueue, ^{
//        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.lingyu.me/wp-content/uploads/2016/08/www.lingyu.me_20160808081654350.jpg"]];
//        UIImage *image = [UIImage imageWithData:data];
//        self.bottomImage = image;
//    });
//    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
//        NSLog(@"两张图片已经下载完毕");
//        _topImageView.image = _topImage;
//        _bottomImageView.image = _bottomImage;
//    });
    //
    dispatch_async(concurrentQueue, ^{
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://pic1.win4000.com/wallpaper/8/511ce1c9ee276.jpg"]];
        UIImage *image = [UIImage imageWithData:data];
        dispatch_async(dispatch_get_main_queue(), ^{
            _topImageView.image = image;
        });
    });
    dispatch_async(concurrentQueue, ^{
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.lingyu.me/wp-content/uploads/2016/08/www.lingyu.me_20160808081654350.jpg"]];
        UIImage *image = [UIImage imageWithData:data];
        dispatch_async(dispatch_get_main_queue(), ^{
            _bottomImageView.image = image;
        });
    });
}

// 同步串行队列
- (void)syncSerialQueue:(dispatch_queue_t)queue {
    // block中的内容会追加到队列中
    NSLog(@"同步串行队列执行任务");
    for (int i = 0; i < 10; i++) {
        dispatch_sync(queue, ^{
            NSLog(@"%d-----%@", i, [NSThread currentThread]);
        });
    }
}
// 同步并发队列
- (void)syncConcurrentQueue:(dispatch_queue_t)queue {
    NSLog(@"同步并发队列执行任务");
    for (int i = 0; i < 10; i++) {
        dispatch_sync(queue, ^{
            NSLog(@"%d-----%@", i, [NSThread currentThread]);
        });
    }
}
// 同步主队列
- (void)syncMainQueue:(dispatch_queue_t)queue {
    NSLog(@"同步主队列执行任务");
    for (int i = 0; i < 10; i++) {
        dispatch_sync(queue, ^{
            NSLog(@"%d-----%@", i, [NSThread currentThread]);
        });
    }
}
// 同步全局队列
- (void)syncGlobalQueue:(dispatch_queue_t)queue {
    NSLog(@"同步全局队列执行任务");
    for (int i = 0; i < 10; i++) {
        dispatch_sync(queue, ^{
            NSLog(@"%d-----%@", i, [NSThread currentThread]);
        });
    }
}
// 异步串行队列, 创建一个新的线程, 在该线程中执行
- (void)asyncSerialQueue:(dispatch_queue_t)queue {
    NSLog(@"异步串行队列执行任务");
    for (int i = 0; i < 10; i++) {
        dispatch_async(queue, ^{
            NSLog(@"%d-----%@", i, [NSThread currentThread]);
        });
    }
}
// 异步并发队列
- (void)asyncConcurrentQueue:(dispatch_queue_t)queue {
    NSLog(@"异步并发队列执行任务");
    for (int i = 0; i < 10; i++) {
        dispatch_async(queue, ^{
            NSLog(@"%d-----%@", i, [NSThread currentThread]);
        });
    }
}
// 异步主队列
- (void)asyncMainQueue:(dispatch_queue_t)queue {
    NSLog(@"异步主队列执行任务");
    for (int i = 0; i < 10; i++) {
        dispatch_async(queue, ^{
            NSLog(@"%d-----%@", i, [NSThread currentThread]);
        });
    }
}
// 异步全局队列
- (void)asyncGlobalQueue:(dispatch_queue_t)queue {
    NSLog(@"异步全局队列执行任务");
    for (int i = 0; i < 10; i++) {
        dispatch_async(queue, ^{
            NSLog(@"%d-----%@", i, [NSThread currentThread]);
        });
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
